﻿using System;
using System.Reflection;

namespace Hxsoft.AppV1
{
	public class UserIntegration
	{
		private static IUserIntegration[] _userIntegrationObjects;

		/// <summary>
		/// 用户整合对象。
		/// </summary>
		public static IUserIntegration[] UserIntegrationObjects
		{
			get
			{
				if (_userIntegrationObjects == null)
				{
					UserIntegrationSubsystemElementCollection subsystems = ApplicationConfiguration.GetUserIntegrationSubsystems();
					if (subsystems != null && subsystems.Count > 0)
					{
						_userIntegrationObjects = new IUserIntegration[subsystems.Count];
						for (int i = 0; i < subsystems.Count; i++)
						{
							UserIntegrationSubsystemElement subsystem = subsystems[i];
							UserIntegrationObjects[i] = (IUserIntegration)Assembly.Load(subsystem.AssemblyName).CreateInstance(subsystem.ClassName);
						}
					}
				}

				return _userIntegrationObjects;
			}
		}

		public static bool ExistsIntegrationSubsystemUser(string sourceSystemName, string userName)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return false;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				if (obj.ExistsUser(userName))
					return true;
			}

			return false;
		}

		public static void CreateIntegrationSubsystemUser(string sourceSystemName, string userName, string password, string email)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.CreateUser(userName, password, email);
			}
		}

		public static void ChangeIntegrationSubsystemUserPassword(string sourceSystemName, string userName, string password)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.ChangeUserPassword(userName, password);
			}
		}

		public static void ChangeIntegrationSubsystemUserPassword(string sourceSystemName, string userName, string password,string newpassword, string email)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.ChangeUserPassword(userName, password, newpassword, email);
			}
		}

		public static void DeleteIntegrationSubsystemUser(string sourceSystemName, string userName)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.DeleteUser(userName);
			}
		}

		public static void LoginIntegrationSubsystem(string sourceSystemName, string userName, string password)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.Login(userName, password);
			}
		}

		public static void LoginNotValidateIntegrationSubsystem(string sourceSystemName, string userName)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.LoginNotValidate(userName);
			}
		}

		public static void LogoutIntegrationSubsystem(string sourceSystemName, string userName)
		{
			IUserIntegration[] userIntegrationObjects = UserIntegrationObjects;
			if (userIntegrationObjects == null) return;

			foreach (IUserIntegration obj in userIntegrationObjects)
			{
				if (obj.Name == sourceSystemName)
					continue;

				obj.Logout(userName);
			}
		}
	}
}
